import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/components/base_components/base_filter_select_chip.dart';
import 'package:x3_component_core/components/base_components/base_text_field.dart';
import 'package:x3_component_core/components/selector_components/base_filter_selector.dart';
import 'package:x3_component_core/models/base_advanced_filter_model.dart';
import 'package:x3_component_core/utils/base_component_utils.dart';

class BaseFilterInputRange extends StatefulWidget {
  final AdvancedFilterModel model;
  final Map<String, dynamic> dataSource;
  const BaseFilterInputRange({Key key, @required this.model, @required this.dataSource})
      : assert(model != null),
        assert(dataSource != null),
        super(key: key);
  @override
  _BaseFilterInputRangeState createState() => _BaseFilterInputRangeState();
}

class _BaseFilterInputRangeState extends State<BaseFilterInputRange> {
  AdvancedFilterModel get model => widget.model;
  Map<String, dynamic> get _dataSource => widget.dataSource;
  List _currentAmountRange = [];
  String _startValue = '';
  String _endValue = '';
  final List<Map> _listOption = [
    {'label': '不限', 'type': 'all', 'value': null},
    {'label': '自定义', 'type': 'custom', 'value': ''},
  ];

  @override
  Widget build(BuildContext context) {
    bool isAll = _dataSource[model.tagId] == null ? true : false;
    if (!isAll) _currentAmountRange = _dataSource[model.tagId]['amountRange'];
    String _selectedText = isAll == true ? '不限' : _currentAmountRange.join(' ~ ');
    List<Widget> _children = [];
    for (Map item in _listOption) {
      _children.add(BaseFilterSelectChip(
        selected: _onIsSelected(item),
        label: item['label'],
        onSelected: (bool) {
          _onSelected(item);
        },
      ));
    }
    return BaseFilterSelector(
      title: model.tagName,
      selectedText: _selectedText,
      children: _children,
      isExpanded: model.tagIsExpanded,
      onChange: (bool state) {
        model.tagIsExpanded = state;
      },
    );
  }

  bool _onIsSelected(Map item) {
    if (_dataSource[model.tagId] == item['value']) return true;
    return item['value'].toString() == _dataSource[model.tagId].toString();
  }

  void _onSelected(Map item) {
    switch (item['type']) {
      case 'all':
        setState(() {
          _dataSource[model.tagId] = null;
        });
        break;
      case 'custom':
        _showInputRangeDialog();
        break;
    }
  }

  void _showInputRangeDialog() {
    String title = '请输入${model.tagName}范围';
    BaseComponentUtils.showCommonDialog(
      context,
      titleWidget: Container(
        padding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
        child: Text(title),
      ),
      contentWidget: _buildShowInputRangeDialog(),
      onOKPress: () {
        if (_startValue.isEmpty || _endValue.isEmpty) return BaseUtils.showToast('${model.tagName}不能为空');
        _listOption[1]['value'] = {
          "rangeType": 5,
          "rangeTypeName": null,
          "amount": null,
          "amountRange": [_startValue, _endValue]
        };
        _setInputRange();
        Navigator.pop(context);
      },
    );
  }

  Widget _buildShowInputRangeDialog() {
    return Container(
      child: Row(
        children: [
          Expanded(
            child: Container(
              alignment: Alignment.centerRight,
              child: BaseTextField(onChanged: (value) => {_startValue = value}),
            ),
          ),
          Container(padding: EdgeInsets.symmetric(horizontal: 8), child: Text(' ~ ')),
          Expanded(
            child: BaseTextField(onChanged: (value) => {_endValue = value}),
          ),
        ],
      ),
    );
  }

  void _setInputRange() {
    _dataSource[model.tagId] = {
      "rangeType": 5,
      "rangeTypeName": null,
      "amount": null,
      "amountRange": [_startValue, _endValue]
    };
    setState(() {});
  }
}
